/* Follow procedure:
1) Create a corresponding .h file
2) Edit the Makefile
2.5) 'make cycle', 'make clean', checkin
3) Create a test file
4) edit the make file
5) 'make cycle', 'make clean', checkin

use 'make shift' and 'make cycle' liberally while coding

*/


#include "Argument.h"

pArgument Argument_new()
{
	pArgument ret = (pArgument) GC_malloc(sizeof(Argument));
	ret->stringLiteral = String_new(NULL);
	ret->parameterVariable = NULL;
	ret->statement = NULL;
	ret->eval = &Argument_eval;
	ret->toString = &Argument_toString;
	return ret;
}

pArgument Argument_newFromCString(char* cString)
{
	pArgument ret = Argument_new();
	if (cString[0] == '$')
	{ //parameterVariable
		ret->stringLiteral = NULL;
		ret->parameterVariable = String_new(cString);
	} else
	{
		ret->stringLiteral = String_new(cString);
	}
	return ret;
}

pArgument Argument_newFromAnonymousStatement(struct AnonymousStatementTag* statement)
{
	pArgument ret = Argument_new();
	ret->stringLiteral = NULL;
	ret->statement = statement->clone(statement);
	return ret;
}

pString Argument_eval(pArgument argument, pAnonymousStatement owner)
{
	if (argument->stringLiteral != NULL)
	{
		pString ret = argument->stringLiteral;
		ret = String_substring(ret, 1, ret->length - 1);
		return ret;
	}
	if (argument->parameterVariable != NULL)
		return NULL; // do some crazy symbol lookup
	if (argument->statement != NULL)
		return argument->statement->eval(argument->statement);
}

ArgumentList Argument_cloneList(ArgumentList list)
{
	return NULL;
}

pString Argument_toString(pArgument argument)
{
	if (argument->stringLiteral) return argument->stringLiteral;
	if (argument->parameterVariable) return argument->parameterVariable;
	if (argument->statement)
	{
		return argument->statement->toString(argument->statement);
	}
}

pString ArgumentList_toString(ArgumentList list)
{
	pArgument current = list[0];
	int i = 0;
	pString ret = String_new(NULL);
	while(current != NULL)
	{
		ret = ret->concat(ret, current->toString(current));
		ret = ret->concat(ret, String_new(", "));
		i++;
		current = list[i];
	}
	return ret;
}

int ArgumentList_getLength(ArgumentList list)
{
	if (list == NULL) return 0;
	pArgument current = list[0];
	int i = 0;
	while (current != NULL) current = list[++i];
	return i;
}
